<!-- 用户信息列表 -->
<template>
  <div class="main-wrapper">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="名称"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="状态"
        clearable
        class="filter-item"
      >
        <el-option
          v-for="item in statusOptions"
          :key="item.key"
          :label="item.statusName"
          :value="item.key"
        />
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
      <!-- <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button> -->
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="用户ID" align="center" width="80">
        <template slot-scope="{ row }">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" width="150">
        <template slot-scope="{ row }">
          {{ row.name }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="头像/logo" align="center" width="88">
        <template slot-scope="{ row }">
          <el-image
            v-if="row.avatar"
            class="avatar"
            :src="getAvatarUrl(row.avatar)"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="所在省ID" align="center">
        <template slot-scope="{ row }">
          {{ row.provinceName }}{{ row.cityName }}
        </template>
      </el-table-column>
      <el-table-column label="服务等级" align="center" width="80">
        <template slot-scope="{ row }">
          {{ row.serviceLevel }}
        </template>
      </el-table-column>
      <el-table-column label="手机" align="center" width="120">
        <template slot-scope="{ row }">
          {{ row.phoneNo }}
        </template>
      </el-table-column>
      <el-table-column label="是否代理" align="center" width="80">
        <template slot-scope="{ row }">
          <i
            v-if="row.agentType === 1"
            class="el-icon-success"
            style="color: #42b983"
          />
        </template>
      </el-table-column>
      <el-table-column label="推荐人" align="center" width="120">
        <template slot-scope="{ row }">
          <el-tooltip v-if="row.referrerUserName" effect="dark" placement="top">
            <template #content>
              id:{{ row.referrerUserId }}
            </template>
            <div class="no-select" @dblclick="openReferrerEditDialog(row)">
              {{ row.referrerUserName }}
            </div>
          </el-tooltip>
          <el-button
            v-else
            type="primary"
            size="small"
            @click="openReferrerEditDialog(row)"
          >
            添加
          </el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="经营范围" align="center">
        <template slot-scope="{row}">
          {{ row.busiScope }}
        </template>
      </el-table-column>
      <el-table-column label="专业范围" align="center">
        <template slot-scope="{row}">
          {{ row.profScope }}
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" width="132">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="80">
        <template slot-scope="{ row }">
          <el-tag :type="row.status | styleFilter()">{{
            row.status | statusFilter()
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="138">
        <template slot-scope="{ row }">
          <router-link :to="'/user/detail/' + row.id"
            ><el-button size="mini" type="primary">详情</el-button></router-link
          >
          <el-button
            v-if="row.status === 0 || row.status === 3"
            size="mini"
            type="success"
            @click="checkUser(row, 1)"
            >审核通过</el-button
          >
          <el-button
            v-if="row.status === 0 || row.status === 3"
            size="mini"
            type="warning"
            @click="checkUser(row, 3)"
            >不通过</el-button
          >
          <el-button
            v-if="row.status === 1"
            size="mini"
            type="danger"
            @click="lockUser(row)"
            >锁定</el-button
          >
          <el-button
            v-if="row.status === 2"
            size="mini"
            type="success"
            @click="reuseUser(row, 1)"
            >恢复</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="fetchUserList"
    />

    <el-dialog
      title="修改用户推荐人"
      :visible.sync="showReferrerEditDialog"
      width="200px"
      :close-on-click-modal="false"
      center
      @close="closeReferrerEditDialog"
    >
      <label>推荐人ID</label>
      <el-input
        v-model="referrerEditForm.referrerUserId"
        placeholder="输入推荐人ID"
      />
      <span slot="footer">
        <el-button type="primary" @click="handleReferrerUpdate">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, editUserStatus, editUser } from '@/api/user'
import { getImageUrl, parseTime } from '@/utils'
import Pagination from '@/components/Pagination'

export default {
  name: 'UserInfo',
  components: {
    Pagination
  },
  filters: {
    styleFilter(style) {
      const styleMap = { 1: 'success', 0: 'danger' }
      return styleMap[style]
    },
    timeFilter(time, format) {
      return parseTime(time, format)
    },
    statusFilter(value) {
      const statusMap = { 0: '无效', 1: '有效', 2: '锁定' }
      return statusMap[value]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20
      },
      statusOptions: [
        { key: 0, statusName: '无效' },
        { key: 1, statusName: '有效' }
      ],
      userData: {},
      referrerEditForm: {
        id: null,
        referrerUserId: null
      },
      showReferrerEditDialog: false
    }
  },
  created() {
    this.fetchUserList()
  },
  methods: {
    fetchUserList() {
      this.listLoading = true
      getUserList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.fetchUserList()
    },
    lockUser(row) {
      row.status = 2
      this.updateUserStatus(row, 2)
    },
    reuseUser(row) {
      row.status = 1
      this.updateUserStatus(row, 1)
    },
    checkUser(row, status) {
      row.status = status
      this.updateUserStatus(row, status)
    },
    updateUserStatus(row, status) {
      row.status = status
      editUserStatus(row.id, status).then(() => {
        this.userData = Object.assign({}, row)
        this.userData.status = status
        for (const v of this.list) {
          if (v.id === this.userData.id) {
            const index = this.list.indexOf(v)
            this.list.splice(index, 1, this.userData)
            break
          }
        }
      })
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    },
    getAvatarUrl(avatar) {
      return getImageUrl(avatar)
    },
    openReferrerEditDialog(user) {
      this.referrerEditForm.id = user.id
      this.referrerEditForm.referrerUserId = user.referrerUserId
      this.showReferrerEditDialog = true
    },
    closeReferrerEditDialog() {
      this.referrerEditForm.id = null
      this.referrerEditForm.referrerUserId = null
    },
    handleReferrerUpdate() {
      this.$confirm(` 请再次确认修改推荐人`, '确认', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.updateReferrer()
      })
    },
    updateReferrer() {
      editUser(this.referrerEditForm)
        .then(res => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
        })
        .catch(err => {
          console.log('err :>> ', err)
        })
        .finally(() => {
          this.showReferrerEditDialog = false
          this.closeReferrerEditDialog()
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.main-wrapper {
  margin: 8px;
  .filter-container {
    padding: 10px 0;
    .filter-item {
      width: 200px;
      margin-right: 10px;
    }
  }
  .avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: 1px solid #f0f0f0;
  }

  .no-select {
    -webkit-touch-callout: none; /*系统默认菜单被禁用*/
    -webkit-user-select: none; /*webkit浏览器*/
    -khtml-user-select: none; /*早期浏览器*/
    -moz-user-select: none; /*火狐*/
    -ms-user-select: none; /*工E10*/
    user-select: none;
  }
}
</style>
